﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using TTGameEngine;

/*
 * PuppetPropertyEditor
 *
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 9/2/2017 4:05:25 PM
 */
[CustomEditor(typeof(PuppetProperty))]
[CanEditMultipleObjects]
public class PuppetPropertyEditor : Editor {
    private SerializedObject scriptObj;
    private PuppetProperty obj;

    SerializedProperty m_intProps;
    SerializedProperty m_floatProps;
    SerializedProperty m_strProps;

    SerializedProperty myCampID;//初始阵营

    void OnEnable() {
        scriptObj = new SerializedObject(target);
        obj = target as PuppetProperty;
        m_intProps = scriptObj.FindProperty("m_intProps");
        m_floatProps = scriptObj.FindProperty("m_floatProps");
        m_strProps = scriptObj.FindProperty("m_strProps");
        myCampID = scriptObj.FindProperty("myCampID");

        FlodoutFlags[0] = true;
        FlodoutFlags[1] = true;

        resizePropArraySize();
    }

    bool[] FlodoutFlags = new bool[10];

    public override void OnInspectorGUI() {
        resizePropArraySize();

        scriptObj.Update();

        EditorGUILayout.PropertyField(myCampID);

        FlodoutFlags[0] = EditorGUILayout.Foldout(FlodoutFlags[0], "PuppetProp");
        if(FlodoutFlags[0]) {
            int indent = EditorGUI.indentLevel;
            EditorGUI.indentLevel++;
            for(int i = 0; i < m_intProps.arraySize; i++) {
                string label = string.Format("[{0}]{1}", i, ((PuppetIntPropID)i).ToString());
                EditorGUILayout.PropertyField(m_intProps.GetArrayElementAtIndex(i), new GUIContent(label));
            }
            EditorGUI.indentLevel = indent;
        }

        FlodoutFlags[1] = EditorGUILayout.Foldout(FlodoutFlags[1], "PuppetFloatProp");
        if(FlodoutFlags[1]) {
            int indent = EditorGUI.indentLevel;
            EditorGUI.indentLevel++;
            for(int i = 0; i < m_floatProps.arraySize; i++) {
                string label = string.Format("[{0}]{1}", i + Puppet.BUFF_FLOAT_PROP_BEGIN, ((PuppetFloatPropID)i).ToString());
                EditorGUILayout.PropertyField(m_floatProps.GetArrayElementAtIndex(i), new GUIContent(label));
            }
            EditorGUI.indentLevel = indent;
        }

        FlodoutFlags[2] = EditorGUILayout.Foldout(FlodoutFlags[1], "PuppetStrProp");
        if(FlodoutFlags[2]) {
            int indent = EditorGUI.indentLevel;
            EditorGUI.indentLevel++;
            for(int i = 0; i < m_strProps.arraySize; i++) {
                string label = string.Format("[{0}]{1}", i, ((PuppetStrPropID)i).ToString());
                EditorGUILayout.PropertyField(m_strProps.GetArrayElementAtIndex(i), new GUIContent(label));
            }
            EditorGUI.indentLevel = indent;
        }

        FlodoutFlags[3] = EditorGUILayout.Foldout(FlodoutFlags[3], "All Values");
        if(FlodoutFlags[3]) {
            base.OnInspectorGUI();
        }

        scriptObj.ApplyModifiedProperties();
    }

    void resizePropArraySize() {
        if(obj.m_intProps.Length != (int)PuppetIntPropID.PropCount) {
            int[] props = new int[(int)PuppetIntPropID.PropCount];
            int len = Mathf.Min(obj.m_intProps.Length, props.Length);
            for(int i = 0; i < len; i++) {
                props[i] = obj.m_intProps[i];
            }
            obj.m_intProps = props;
        }
        if(obj.m_floatProps.Length != (int)PuppetFloatPropID.PropCount) {
            float[] props = new float[(int)PuppetFloatPropID.PropCount];
            int len = Mathf.Min(obj.m_floatProps.Length, props.Length);
            for(int i = 0; i < len; i++) {
                props[i] = obj.m_floatProps[i];
            }
            obj.m_floatProps = props;
        }
        if(obj.m_strProps.Length != (int)PuppetStrPropID.PropCount) {
            string[] props = new string[(int)PuppetStrPropID.PropCount];
            int len = Mathf.Min(obj.m_strProps.Length, props.Length);
            for(int i = 0; i < len; i++) {
                props[i] = obj.m_strProps[i];
            }
            obj.m_strProps = props;
        }
    }
}